<?php 

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class AdminController extends Controller {

    //后台首页
    public function index() {
        $admin = \DB::table('admin')->paginate(15);
        return view('admin.admin_list')->with('data',$admin);
    }

    //后台管理员修改页面
    public function edit($id) {
         $data=\DB::table("admin")->where("id",$id)->first();
          return view('admin.admin_edit')->with('data',$data);
    }

    //后台添加管理员页面 admin/admin/create 
    public function create() {
        return view('admin.admin_add');
    }

    //插入操作 post admin/admin post
    public function store(Request $request) { 
        //获取数据
        $str = $_POST['str'];
        //将表单序列化的值转换成数组
        parse_str($str, $arr);
        $rule = [
            'name' => 'required|unique:admin|between:6,12',
            'pass' => 'required|same:repass|between:6,12',
        ];
        $message = [
            'name.required' => "请输入用户名",
            'name.unique' => "用户名已经存在",
            'name.between' => "用户名长度应在6-12个字符之间",
            'pass.required' => "请输入密码",
            'pass.same' => "两次密码输入不一致",
            'pass.between' => "密码长度应在6-12个字符之间",
        ];
        $validator = \Validator::make($arr, $rule, $message);
        if ($validator->passes()) {
            $arr['time'] = time();
            unset($arr['repass']);
            if (\DB::table("admin")->insert($arr)) {
                 echo 1;
            }
        } else {
            return $validator->getMessageBag()->getMessages();
        }
    }

    //后台删除管理员页面 admin/admin/{admin}
    public function destroy($id) {
          if(\DB::table("admin")->where("id",$id)->delete()){
              return 1;
          }else{
              return 0;
          }
      }

    //后台修改管理员页面 admin/admin/{admin} put
    public function update(Request $request) {
          //获取数据 
        $str = $_POST['str'];
        //将表单序列化的值转换成数组
        parse_str($str, $arr); 
        $rule = [ 
            'pass' => 'required|same:repass|between:6,12',
        ];
        $message = [  
            'pass.required' => "请输入密码",
            'pass.same' => "两次密码输入不一致",
            'pass.between' => "密码长度应在6-12个字符之间",
        ];
        $validator = \Validator::make($arr, $rule, $message);
        if ($validator->passes()) { 
            unset($arr['repass']);
            if (\DB::table("admin")->where("id",$arr['id'])->update($arr)) {
                return 1;
            }
        } else {
            return $validator->getMessageBag()->getMessages();
        }
    }
    
    public function AjaxStatus(Request $request){
         $arr=$request->except('_token');  
         if(\DB::update("update admin set status=".$arr['status']." where id=".$arr['id'])){
             return 1;
         }else{
             return 0;
         }
    }



//    +--------+-----------+--------------------------+---------------+----------------------------------------------------+--------------+
//| Domain | Method    | URI                      | Name          | Action                                             | Middleware   |
//+--------+-----------+--------------------------+---------------+----------------------------------------------------+--------------+
//|        | GET|HEAD  | admin                    |               | App\Http\Controllers\Admin\IndexController@index   | web          |
//|        | GET|HEAD  | admin/admin              | admin.index   | App\Http\Controllers\Admin\AdminController@index   | web          |
//|        | POST      | admin/admin              | admin.store   | App\Http\Controllers\Admin\AdminController@store   | web          |
//|        | GET|HEAD  | admin/admin/create       | admin.create  | App\Http\Controllers\Admin\AdminController@create  | web          |
//|        | DELETE    | admin/admin/{admin}      | admin.destroy | App\Http\Controllers\Admin\AdminController@destroy | web          |
//|        | PUT|PATCH | admin/admin/{admin}      | admin.update  | App\Http\Controllers\Admin\AdminController@update  | web          |
//|        | GET|HEAD  | admin/admin/{admin}      | admin.show    | App\Http\Controllers\Admin\AdminController@show    | web          |
//|        | GET|HEAD  | admin/admin/{admin}/edit | admin.edit    | App\Http\Controllers\Admin\AdminController@edit    | web          |
//|        | GET|HEAD  | api/user                 |               | Closure                                            | api,auth:api |
//+--------+-----------+--------------------------+---------------+----------------------------------------------------+--------------+
}
